package com.ocom.xfrecode.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import com.ocom.common.utils.RandomUtil;
import com.ocom.xfrecode.entity.Test;
import com.ocom.xfrecode.mongo.po.EntityDeviceHistory;
import com.ocom.xfrecode.mongo.po.MyEntity;
import com.ocom.xfrecode.mongo.service.DeviceHistoryService;
import com.ocom.xfrecode.mongo.service.MyEntityService;
import com.ocom.xfrecode.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class TestController {

	@Autowired
	private TestService testService;
	@Autowired
	private IdGenerator idGenerator;


	@GetMapping("/test")
	public int yt(){
		List<Test> entitys=new ArrayList<>();
		Random random=new Random();
		for(int i=0;i<10;i++){
			Test goods=new Test();
			goods.setId(idGenerator.generateId().intValue());
			int rand=random.nextInt(9);
			goods.setMonthValue(Integer.valueOf("202201"+rand));
			goods.setYearValue(2022);
			entitys.add(goods);
		}

		return testService.batchAdd(entitys);
	}


	@GetMapping("/list2")
	public List<Test> list(){
		QueryWrapper<Test> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("month_value",2022011).or().eq("month_value",2022017);
		return testService.findByGoods(queryWrapper);
	}


	@GetMapping("/test3")
	public int yt5(){
		System.out.println(1);
		return 1;
	}

	@Resource
	MyEntityService myEntityService;

	@Resource
	DeviceHistoryService deviceHistoryService;

	@GetMapping("/test4")
	public int yt53(){
		MyEntity myEntity=new MyEntity();
		myEntity.setField1(RandomUtil.getRandomString(6));
		myEntity.setField2(97);
		myEntityService.save(myEntity);
		return 1;
	}

	@GetMapping("/test5")
	public Object test04(){
		EntityDeviceHistory entityDeviceHistory = new EntityDeviceHistory();
		entityDeviceHistory.setId(idGenerator.generateId().longValue());
		entityDeviceHistory.setDeviceNo(cn.hutool.core.util.RandomUtil.randomNumbers(5));
		return deviceHistoryService.save(entityDeviceHistory);
	}
}
